﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FXCopAddIn.Windows
{
    public partial class frmSettings : Form
    {
        #region Fields

        private string _FXCopPath;
        private BaseAddIn.ProjectInfo.RuleFilesList _rules;
        private List<string> _doubleRules;

        #endregion

        #region Construtor

        public frmSettings()
        {
            InitializeComponent();

            _FXCopPath = null;
            _rules = null;
        }

        #endregion

        #region Event Handlers

        private void btnOk_Click(object sender, EventArgs e)
        {
            int index;
            BaseAddIn.ProjectInfo.RuleFile ruleFile;
            BaseAddIn.ProjectInfo.Rule rule;
            bool todasRulesEnabled;

            foreach (Classes.ThreeStateTreeNode nodeRuleFile in trvRules.Nodes)
            {
                todasRulesEnabled = true;
                index = _rules.IndexOf((BaseAddIn.ProjectInfo.RuleFile)nodeRuleFile.Tag);
                ruleFile = _rules[index];

                if (nodeRuleFile.Estado == Classes.Enums.ThreeState.Checked)
                    ruleFile.Enabled = true;
                else
                    ruleFile.Enabled = false;

                foreach (Classes.ThreeStateTreeNode nodeRule in nodeRuleFile.Nodes)
                {
                    index = ruleFile.Rules.IndexOf((BaseAddIn.ProjectInfo.Rule)nodeRule.Tag);
                    rule = ruleFile.Rules[index];

                    if (String.IsNullOrEmpty(rule.NameDoubleRule))
                    {
                        if (nodeRule.Estado == Classes.Enums.ThreeState.Checked)
                            rule.Enabled = true;
                        else
                            rule.Enabled = false;
                    }
                    else
                    {
                        foreach (BaseAddIn.ProjectInfo.Rule nodeDoubleRule in ruleFile.Rules)
                        {
                            if (nodeDoubleRule.NameDoubleRule == rule.NameDoubleRule)
                            {
                                if (nodeRule.Estado == Classes.Enums.ThreeState.Checked)
                                    nodeDoubleRule.Enabled = true;
                                else
                                    nodeDoubleRule.Enabled = false;
                            }
                        }
                    }

                    if (!rule.Enabled)
                        todasRulesEnabled = false;
                }

                ruleFile.AllRulesEnabled = todasRulesEnabled;
            }

            Properties.Settings.Default.CaminhoFXCop = _FXCopPath;
            Properties.Settings.Default.Rules = _rules;
            Properties.Settings.Default.Save();

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnPathFXCop_Click(object sender, EventArgs e)
        {
            if (ofdFXCOp.ShowDialog(this) == DialogResult.OK)
            {
                _FXCopPath = ofdFXCOp.FileName;
                txtPathFXCop.Text = _FXCopPath;
                ofdFXCOp.InitialDirectory = Path.GetDirectoryName(_FXCopPath);
            }
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            _FXCopPath = Properties.Settings.Default.CaminhoFXCop;
            _rules = Properties.Settings.Default.Rules;
            _doubleRules = new List<string>();
            txtPathFXCop.Text = _FXCopPath;
            ofdFXCOp.InitialDirectory = Path.GetDirectoryName(_FXCopPath);
            ofdFXCOp.FileName = _FXCopPath;

            CarregaTreeView();
        }

        #endregion

        #region Public Methods

        public static void Execute(ref List<BaseAddIn.Rules.BaseRuleAddIn> _selectedRulesAddin)
        {
            frmSettings frm = new frmSettings();

            if (frm.ShowDialog() == DialogResult.OK)
            {
                 BaseAddIn.Load.CarregaInstanciaRulesAddIn(Properties.Settings.Default.CaminhoRules,
                                                           ref _selectedRulesAddin,
                                                           Properties.Settings.Default.Rules);
            }

            frm.Dispose();
        }

        #endregion

        #region Private Methods

        private void CarregaTreeView()
        {
            Classes.ThreeStateTreeNode nodeRuleFile;
            Classes.ThreeStateTreeNode nodeRule;
            string tipo = typeof(BaseAddIn.Rules.BaseRuleAddIn).FullName;
            bool undefined;

            foreach (BaseAddIn.ProjectInfo.RuleFile ruleFile in _rules)
            {
                undefined = false;

                nodeRuleFile = new Classes.ThreeStateTreeNode();
                nodeRuleFile.Text = ruleFile.Name;                
                nodeRuleFile.ImageIndex = 0;
                nodeRuleFile.SelectedImageIndex = nodeRuleFile.ImageIndex;
                nodeRuleFile.Tag = ruleFile;

                foreach (BaseAddIn.ProjectInfo.Rule rule in ruleFile.Rules)
                {
                    nodeRule = new Classes.ThreeStateTreeNode();

                    if (!String.IsNullOrEmpty(rule.NameDoubleRule))
                    {
                        if (_doubleRules.Contains(rule.NameDoubleRule))
                        {
                            continue;
                        }
                        else
                        {
                            nodeRule.Text = rule.NameDoubleRule;                            
                            nodeRule.ImageIndex = 1;

                            _doubleRules.Add(rule.NameDoubleRule);
                        }
                    }
                    else
                    {
                        nodeRule.Text = rule.Name;
                        nodeRule.ImageIndex = (rule.TipoRegra == tipo) ? 2 : 1;                        
                    }

                    nodeRule.SelectedImageIndex = nodeRule.ImageIndex;

                    if (rule.Enabled)
                    {
                        nodeRule.Estado = Classes.Enums.ThreeState.Checked;
                        undefined = true;
                    }
                    else
                    {
                        nodeRule.Estado = Classes.Enums.ThreeState.Unchecked;
                    }
                    
                    nodeRule.Tag = rule;

                    nodeRuleFile.Nodes.Add(nodeRule);
                }

                if (ruleFile.Enabled)
                {
                    nodeRuleFile.Estado = Classes.Enums.ThreeState.Checked;
                }
                else
                {
                    if (undefined)
                    {
                        nodeRuleFile.Estado = Classes.Enums.ThreeState.Undefined;
                    }
                    else
                    {
                        nodeRuleFile.Estado = Classes.Enums.ThreeState.Unchecked;
                    }
                }

                trvRules.Nodes.Add(nodeRuleFile);
            }
        }

        #endregion
    }
}
